package com.spring.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.spring.domain.Students;

@Repository
public class StudentsDAOImpl implements StudentsDAO {

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	public Students getStudent(int id) {
		Students student = (Students) sessionFactory.getCurrentSession().get(Students.class, id);
		return student;
	}

	@Override
	public List<Students> getListStudents() {
		List<Students> result = sessionFactory.getCurrentSession().createCriteria(Students.class).list();
		return result;
	}

	@Override
	public boolean saveStudent(Students s) {
		sessionFactory.getCurrentSession().save(s);
		return true;
	}

	@Override
	public boolean deleteStudent(Students s) {
		sessionFactory.getCurrentSession().delete(s);
		return true;
	}

	@Override
	public boolean updateStudent(Students s) {
		try {
			sessionFactory.getCurrentSession().update(s);
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}

}
